#include "wcr.h"
#include "rectype.h"
#include "util.h"
#include "nmfield.h"
#include "nmfield.cpp"

#include "cnfield.h"
#include "cnfield.cpp"

#include "stdffield.h"
#include "stdffield.cpp"

using namespace std;
using namespace opsrc::stdf;

Wcr::Wcr()
{}

Wcr::Wcr(stdf_rec _stdfrec, int _reclen, std::ios::pos_type _fileRecFirstBytePosition, int _orderindex, endian e, char* buf)
:RecBase(_stdfrec, _reclen, _fileRecFirstBytePosition, _orderindex),en(e) {
    if (buf!=0)
        readFields(buf);
}

Wcr::~Wcr() {
}

const StdfField<float> Wcr::getWaferSize() const {
    return wfr_siz;
}

void Wcr::setWaferSize(StdfField<float> wfrsiz) {
    wfr_siz = wfrsiz;
}


const StdfField<float> Wcr::getDieHeight() const {
    return die_ht;
}

void Wcr::setDieHeight(StdfField<float> dieht) {
    die_ht = dieht;
}


const StdfField<float> Wcr::getDieWidth() const {
    return die_wid;
}

void Wcr::setDieWidth(StdfField<float> diewid) {
    die_wid = diewid;
}

const StdfField<int> Wcr::getWaferUnit() const {
    return wfr_unit;
}

void Wcr::setWaferUnit(StdfField<int> wfrunit) {
    wfr_unit = wfrunit;
}


const StdfField<string> Wcr::getWaferFlat() const {
    return wfr_flat;
}

void Wcr::setWaferFlat(StdfField<string> wfrflat) {
    wfr_flat = wfrflat;
}

const StdfField<int> Wcr::getCenterX() const {
    return center_x;
}

void Wcr::setCenterX(StdfField<int> centerx) {
    center_x = centerx;
}

const StdfField<int> Wcr::getCenterY() const {
    return center_y;
}

void Wcr::setCenterY(StdfField<int> centery) {
    center_y = centery;
}

const StdfField<string> Wcr::getPositionX() const {
    return pos_x;
}

void Wcr::setPositionX(StdfField<string> posx) {
    pos_x = posx;
}

const StdfField<string> Wcr::getPositionY() const {
    return pos_y;
}

void Wcr::setPositionY(StdfField<string> posy) {
    pos_y = posy;
}


void Wcr::readFields(char *buf) {
#if _DEBUG
    cout << "stdfrec= WCR , filebytestartposition= " <<   getFileRecByteStartPosition() << ", recbytecount= " << reclen <<endl;
#endif

    int curpos = 0;
    // Read wafer diamater, float datatype, 4 byte length
    NmField<float, 4> wfrsiz(buf+curpos, en);
    wfr_siz.set(wfrsiz.getData(), curpos, wfrsiz.getByteCount(), wfrsiz.getTotalByteCount());
    curpos += wfr_siz.getTotalByteCount();

    // Read die height, float datatype, 4 byte length
    if (curpos < reclen -1) {
        NmField<float, 4> dieht(buf+curpos, en);
        die_ht.set(dieht.getData(), curpos, dieht.getByteCount(), dieht.getTotalByteCount());
        curpos += die_ht.getTotalByteCount();
    }


    // Read die width, float datatype, 4 byte length
    if (curpos < reclen -1) {
        NmField<float, 4> diewid(buf+curpos, en);
        die_wid.set(diewid.getData(), curpos, diewid.getByteCount(), diewid.getTotalByteCount());

        curpos += die_wid.getTotalByteCount();
    }


    // Read wafer unit, 1 byte. This number is an enumerator
    if (curpos < reclen-1) {
        NmField<int, 1> wfrunit(buf+curpos, en);
        wfr_unit.set(wfrunit.getData(), curpos, wfrunit.getByteCount(), wfrunit.getTotalByteCount());
        curpos += wfr_unit.getTotalByteCount();
    }


    // Read wafer flat, 1 byte. This  is an enumerator
    if (curpos < reclen -1) {
        CnField<string, 1> wfrflat(buf+curpos, en);
        wfr_flat.set(wfrflat.getData(), curpos, wfrflat.getByteCount(), wfrflat.getTotalByteCount());
        curpos += wfr_flat.getTotalByteCount();
    }

    // Read center x, 2 byte
    if (curpos < reclen-1) {
        NmField<int, 2> centerx(buf+curpos, en);
        center_x.set(centerx.getData(), curpos, centerx.getByteCount(), centerx.getTotalByteCount());
        curpos += center_x.getTotalByteCount();
    }


    // Read center y, 2 byte
    if (curpos < reclen -1) {
        NmField<int, 2> centery(buf+curpos, en);
        center_y.set(centery.getData(), curpos, centery.getByteCount(), centery.getTotalByteCount());
        curpos += center_y.getTotalByteCount();
    }


    // Read pos x, 1 byte
    if (curpos < reclen-1) {
        CnField<string, 1> posx(buf+curpos, en);
        pos_x.set(posx.getData(), curpos, posx.getByteCount(), posx.getTotalByteCount());
        curpos += pos_x.getTotalByteCount();
    }


    // Read pos y, 1 byte
    if (curpos < reclen-1) {
        CnField<string, 1> posy(buf+curpos, en);
        pos_y.set(posy.getData(), curpos, posy.getByteCount(), posy.getTotalByteCount());
        curpos += posy.getTotalByteCount();
    }
}
